package com.example.demo.service.imp;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Employee;
import com.example.demo.entity.Flower;
import com.example.demo.entity.Group;
import com.example.demo.mapper.FlowerMapper;
import com.example.demo.service.FlowerService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlowerServiceImp implements FlowerService {
    @Resource
    private FlowerMapper flowerMapper;

    @Override
    public Object selectByUsername(String flowernName) {
        return flowerMapper.selectByUsername(flowernName);
    }

    @Override
    public void insert(Flower flower) {
        flowerMapper.insert(flower);
    }

    @Override
    public void updateById(Flower flower) {
        flowerMapper.updateById(flower);
    }



    @Override
    public void deleteById(Long id) {
        flowerMapper.deleteById(id);
    }

    @Override
    public Page<Flower> selectPage(Integer pageNum, Integer pageSize, String search) {
        //分页查询
        Flower flower = new Flower();
        flowerMapper.selectAll(flower);
        LambdaQueryWrapper<Flower> wrapper = Wrappers.<Flower>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            wrapper.eq(Flower::getFlowerId,search).or();
            wrapper.like(Flower::getFlowerName, search).or();
            wrapper.like(Flower::getGroupName, search).or();
            wrapper.like(Flower::getPrice, search).or();

        }
        Page<Flower> page =flowerMapper.selectPage(new Page<>(pageNum, pageSize),wrapper);
        List<Flower> list =flowerMapper.selectAll(flower);
        for(int i=0;i<page.getRecords().size();i++){
            for(int j=0;j<list.size();j++){
                if(page.getRecords().get(i).getGroupId().equals(list.get(j).getGroupId())){
                    page.getRecords().get(i).setGroupName(list.get(j).getGroupName());
                    flowerMapper.updateById(page.getRecords().get(i));
                }
            }
        }
        return page;
    }

    @Override
    public List<Flower> selectAll(Flower flower) {
        return flowerMapper.selectAll(flower);
    }

    @Override
    public List<Flower> selectUrl(Flower flower) {
        return flowerMapper.selectUrl(flower);
    }

    @Override
    public Object selectGroupPage(String groupId) {
        List<Flower> list=flowerMapper.selectGroup(groupId);
        return list;
    }

    @Override
    public Object selectGroupPage1() {
        List<Flower> list=flowerMapper.selectGroup1();
        return list;
    }

    @Override
    public Flower getFlowerId(String flowerId) {
        return flowerMapper.selectById(flowerId);
    }

    @Override
    public List<Flower> findSearch(String search) {
        return flowerMapper.findSearch(search);
    }
}
